package com.qianli.user.domain.respository;

import com.fqgj.xjd.user.dao.*;
import com.fqgj.xjd.user.entity.*;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.model.UserBehaviorModel;
import com.qianli.user.domain.model.behavior.UserBehaviorDevice;
import com.qianli.user.domain.model.behavior.UserBehaviorGps;
import com.qianli.user.domain.model.behavior.UserBehaviorShumei;
import com.qianli.user.domain.model.behavior.UserBehaviorWifi;
import com.qianli.user.ro.behavior.UserBehaviorLoginRO;
import com.qianli.user.ro.behavior.UserBehaviorRegisterRO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * name:用户行为
 * auth:ting.li@qianli-inc.com
 * date:2018/1/2 20:03
 * version: v1.0.0
 **/
@Repository
public class UserBehaviorRespository implements BaseRespository<UserBehaviorModel> {

    @Autowired
    private UserBehaviorRegisterDao userBehaviorRegisterDao;

    @Autowired
    private UserBehaviorLoginDao userBehaviorLoginDao;

    @Autowired
    private UserBehaviorDeviceDao userBehaviorDeviceDao;

    @Autowired
    private UserBehaviorGpsDao userBehaviorGpsDao;

    @Autowired
    private UserBehaviorDeviceShumeiDao userBehaviorDeviceShumeiDao;

    @Autowired
    private UserBehaviorWifiDao userBehaviorWifiDao;


    @Override
    public void load(UserBehaviorModel model) {
        //app信息

        //设备信息
        UserBehaviorDeviceEntity deviceEntity = userBehaviorDeviceDao.selectDeviceByUserCode(model.getUserCode());
        if (null == deviceEntity) {
            UserBehaviorDevice device = new UserBehaviorDevice();
            BeanUtils.copyProperties(deviceEntity, device);
            model.setUserBehaviorDevice(device);
        }
        //gps
        UserBehaviorGpsEntity gpsEntity = userBehaviorGpsDao.selectLatestUserGpsByUserCode(model.getUserCode());
        if (null != gpsEntity) {
            UserBehaviorGps behaviorGps = new UserBehaviorGps();
            BeanUtils.copyProperties(gpsEntity, behaviorGps);
            model.setUserBehaviorGps(behaviorGps);
        }
        //数美
        UserBehaviorDeviceShumeiEntity shumeiEntity = userBehaviorDeviceShumeiDao.selectByUserCode(model.getUserCode());
        if (null != shumeiEntity) {
            UserBehaviorShumei behaviorShumei = new UserBehaviorShumei();
            BeanUtils.copyProperties(shumeiEntity, behaviorShumei);
            model.setUserBehaviorShumei(behaviorShumei);
        }
        //wifi
        UserBehaviorWifiEntity wifiEntity = userBehaviorWifiDao.selectLatestUserWifiByUserCode(model.getUserCode());
        if (null != wifiEntity) {
            UserBehaviorWifi userBehaviorWifi = new UserBehaviorWifi();
            BeanUtils.copyProperties(wifiEntity, userBehaviorWifi);
            model.setUserBehaviorWifi(userBehaviorWifi);
        }
    }

    @Override
    public void store(UserBehaviorModel model) {
        //app信息
        if (null != model.getAppInfo()) {
            if (model.getAppInfo().getItemStatus().equals(UserModelItemStatusEnum.ADD.getType())) {
                UserBehaviorRegisterEntity registerEntity = new UserBehaviorRegisterEntity();
                BeanUtils.copyProperties(model.getAppInfo(), registerEntity);
                registerEntity.setPlatform(model.getAppInfo().getPlat());
                registerEntity.setClientId(model.getAppInfo().getPushId());
                registerEntity.setRegisterFrom(model.getAppInfo().getFrom());
                registerEntity.setAppCode(String.valueOf(model.getAppInfo().getAppCode()));
                registerEntity.setCreateDate(new Date());
                userBehaviorRegisterDao.insert(registerEntity);
            } else {
                UserBehaviorLoginEntity loginEntity = new UserBehaviorLoginEntity();
                BeanUtils.copyProperties(model.getAppInfo(), loginEntity);
                loginEntity.setPlatform(model.getAppInfo().getPlat());
                loginEntity.setClientId(model.getAppInfo().getPushId());
                loginEntity.setLoginFrom(model.getAppInfo().getFrom());
                userBehaviorLoginDao.insert(loginEntity);
            }
        }
        //设备信息
        if (null != model.getUserBehaviorDevice() &&
                UserModelItemStatusEnum.ADD.getType().equals(model.getUserBehaviorDevice().getItemStatus())) {
            UserBehaviorDevice userBehaviorDevice = model.getUserBehaviorDevice();
            UserBehaviorDeviceEntity deviceEntity = new UserBehaviorDeviceEntity();
            BeanUtils.copyProperties(userBehaviorDevice, deviceEntity);
            deviceEntity.setClientId(model.getAppInfo().getPushId());
            deviceEntity.setPlatform(model.getAppInfo().getPlat());
            deviceEntity.setVersion(model.getAppInfo().getVersion());
            deviceEntity.setVersionCode(model.getAppInfo().getVersionCode());
            userBehaviorDeviceDao.insert(deviceEntity);
        }
        //gps
        if (null != model.getUserBehaviorGps()) {
            UserBehaviorGps behaviorGps = new UserBehaviorGps();
            UserBehaviorGpsEntity gpsEntity = new UserBehaviorGpsEntity();
            BeanUtils.copyProperties(behaviorGps, gpsEntity);
            userBehaviorGpsDao.insert(gpsEntity);
        }
        //数美
        if (null != model.getUserBehaviorShumei()) {
            UserBehaviorShumei shumei = model.getUserBehaviorShumei();
            UserBehaviorDeviceShumeiEntity shumeiEntity = new UserBehaviorDeviceShumeiEntity();
            BeanUtils.copyProperties(shumei, shumeiEntity);
            userBehaviorDeviceShumeiDao.insert(shumeiEntity);
        }
        //wifi
        if (null != model.getUserBehaviorWifi()) {
            UserBehaviorWifi wifi = model.getUserBehaviorWifi();
            UserBehaviorWifiEntity wifiEntity = new UserBehaviorWifiEntity();
            BeanUtils.copyProperties(wifi, wifiEntity);
            userBehaviorWifiDao.insert(wifiEntity);
        }
    }

    @Override
    public void restore(UserBehaviorModel model) {

    }

    public Boolean isNew(UserBehaviorModel newUm) {
        //设备信息
        if (null != newUm.getUserBehaviorDevice()) {
            UserBehaviorDevice behaviorDevice = newUm.getUserBehaviorDevice();
            UserBehaviorDeviceEntity behaviorDeviceEntity = userBehaviorDeviceDao.selectDeviceByUserCode(behaviorDevice.getUserCode());
            if (null == behaviorDeviceEntity) {
                return true;
            }
        }
        return false;
    }

    /**
     * 查询最新登录信息
     *
     * @param userCode
     * @return
     */
    public UserBehaviorLoginRO queryUserLatestBehaviorLogin(String userCode) {
        UserBehaviorLoginEntity loginEntity = userBehaviorLoginDao.queryUserLatestBehaviorLogin(userCode);
        UserBehaviorLoginRO loginRO = null;
        if (null != loginEntity) {
            loginRO = new UserBehaviorLoginRO();
            BeanUtils.copyProperties(loginEntity, loginRO);
            loginRO.setPlat(loginEntity.getPlatform());
            loginRO.setPushId(loginEntity.getClientId());
        }
        return loginRO;
    }

    /**
     * 查询注册信息
     *
     * @param userCode
     * @return
     */
    public UserBehaviorRegisterRO getUserBehaviorRegister(String userCode) {
        UserBehaviorRegisterEntity registerEntity = userBehaviorRegisterDao.selectUserBehaviorRegisterByUserCodeAndAppCode(userCode);
        UserBehaviorRegisterRO registerRO = null;
        if (null != registerEntity) {
            registerRO = new UserBehaviorRegisterRO();
            BeanUtils.copyProperties(registerEntity, registerRO);
            registerRO.setPlat(registerEntity.getPlatform());
            registerRO.setPushId(registerEntity.getClientId());
        }
        return registerRO;
    }
}
